<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/******************************************************************************\
*
* @package      Ajax callback for CodeIgnitor
* @author       Dennis T. Kaplan
* @copyright    Copyright (c) 2007 PzzAzz
* @license		http://www.gnu.org/copyleft/gpl.html
* @name         system/applications/contrller/ajax.php
* @version      1.0 -> 2007-05-10 18:12 -> Created: postcode()
*
\******************************************************************************/

class Ajax extends Controller {
    function __construct() {
        parent::Controller();
    }
    
	function postcode()
	{
        $searchstring = $this->input->get('q');
        $limit = $this->input->get('limit');
        if (!$searchstring) return;
        if ($limit < 1 || $limit > 40) $limit = 10;

        if(strlen($searchstring) > 1){
            if(ereg('[^[:space:]a-zA-Z0-9_.-]{1,}', $searchstring)) {
                echo "Error|Character not allowed.||\n";
            }else{
                $sql  = "SELECT places_postcode.id AS pid, code, city, state, Country ";
                $sql .= "FROM `places_postcode` ";
                $sql .= "JOIN places_country ON places_postcode.country_id = places_country.id ";
                $sql .= "WHERE code LIKE '$searchstring%' ";
                $sql .= "OR `city` ";
                $sql .= "LIKE '$searchstring%' ";
                $sql .= "LIMIT $limit";
                $query = $this->db->query($sql);
                if ($query->num_rows() > 0) {
                    foreach ($query->result() as $v) {
                        echo "$v->pid|$v->code|$v->city|$v->state|$v->Country\n";
                    }
                } else {
                    echo 'Notice|Sorry, no results returned.||';
                }
            }
        }
    }
}
?>
